package com.zang.blogz.controller;

import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.zang.blogz.annotation.OptLog;
import com.zang.blogz.constant.OptTypeConstant;
import com.zang.blogz.entity.UserInfo;
import com.zang.blogz.entity.UserRole;
import com.zang.blogz.model.output.UserMenuVO;
import com.zang.blogz.result.Result;
import com.zang.blogz.service.MenuService;
import com.zang.blogz.service.UserInfoService;
import com.zang.blogz.service.WebsiteConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朵橙i
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/blogz/userInfo")
@Api(tags = "用户信息模块")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MenuService menuService;

    @ApiOperation(value = "获取当前登录用户所拥有的角色",notes = "获取当前登录用户所拥有的角色")
    @GetMapping("/admin/queryListRole")
    public Result<List<String>> getRoleByLoginAdmin() {

        StpInterface stpInterface = SaManager.getStpInterface();


        List<String> roleList = stpInterface.getRoleList(StpUtil.getLoginId(), null);
        return Result.ok(roleList);
    }

    @ApiOperation(value = "获取当前登录用户所拥有的资源",notes = "获取当前登录用户所拥有的资源")
    @GetMapping("/admin/queryListResource")
    public Result<List<String>> getResourceByLoginAdmin() {

        StpInterface stpInterface = SaManager.getStpInterface();

        List<String> permissionList = stpInterface.getPermissionList(StpUtil.getLoginId(), null);

        return Result.ok(permissionList);
    }

    @ApiOperation(value = "对用户进行封号操作")
    @DeleteMapping("/admin/banned")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<String> banned(@RequestParam("day") Integer day){

        return userInfoService.banned((Integer) StpUtil.getLoginId(), day);
    }

    @ApiOperation(value = "更改当前用户所拥有的角色",notes = "更改当前用户所拥有的角色")
    @PostMapping("/admin/role")
    @OptLog(optType = OptTypeConstant.UPDATE)
    public Result<List<UserRole>> updateRole(List<Integer> roles){

        return userInfoService.updateRole(roles);
    }

    @ApiOperation(value = "得到当前登录用户的所有菜单(tree)", notes = "得到当前登录用户的所有菜单(tree)")
    @GetMapping("/admin/queryUserMenu")
    public Result<List<UserMenuVO>> queryUserMenu(){

        return Result.ok(menuService.queryUserMenu());
    }

    @ApiOperation("获取用户个人信息")
    @GetMapping("/admin/getCurrentUserInfo")
    public UserInfo getCurrentUserInfo() {
        return userInfoService.getCurrentUserInfo();
    }

}
